package com.yy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yy.entities.CommonResult;
import com.yy.pojo.Score;
import com.yy.pojo.Student;
import com.yy.service.ClassesService;
import com.yy.service.ScoreService;
import com.yy.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/studentArchives")
public class StudentArchivesController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private ClassesService classesService;

    @Autowired
    private ScoreService scoreService;


    //新增学生信息
    @PostMapping("/add/student/info")
    public CommonResult addStudentInfo(@RequestBody Student student) throws Exception {
        boolean save = studentService.save(student);
        return new CommonResult(200,"添加成功",save);
    }

    //查询所有班级
    @GetMapping("/get/allClasses")
    public CommonResult getAllClasses() {
        return new CommonResult(200,"查询成功",classesService.list());
    }

    //查询三年一班的学生
    @GetMapping("/get/student/archives/{classId}")
    public CommonResult getStudentArchives(@PathVariable("classId") Integer classId) {
        //查询三年一班的学生
        List<Student> students = studentService.list(new QueryWrapper<Student>().eq("class_id", classId));
        List<String> studentNames = new ArrayList<>();
        for (Student student : students) {
            String studentName = student.getStudentName();
            studentNames.add(studentName);
        }
        return new CommonResult(200,"查询成功",studentNames);
    }

    //查询该学生的信息
    @GetMapping("/get/student/info/{studentId}")
    public CommonResult getStudentInfo(@PathVariable("studentId") Integer studentId) {
        Student student=studentService.getOne(new QueryWrapper<Student>().eq("student_id", studentId));
        return new CommonResult(200,"查询成功",student);
    }

    //查询某个学生的成绩
    @GetMapping("/get/student/score/{studentId}")
    public CommonResult getStudentScore(@PathVariable("studentId") Integer studentId,String examinationtype) {
        Score score = scoreService.getOne(new QueryWrapper<Score>().eq("student_id", studentId).eq("examinationtype", examinationtype));
        return new CommonResult(200,"查询成功",score);
    }

    //模糊查询
    @GetMapping("/get/student/info/like")
    public CommonResult getStudentInfoLike(@RequestParam("studentName") String studentName) {
        List<Student> student = studentService.list(new QueryWrapper<Student>().like("student_name", studentName));
        return new CommonResult(200,"查询成功",student);
    }

}
